#include "core/Symbol.hpp"
#include "core/Functions.hpp"
#include "pprint.hpp"
#ifndef BEX_BUILD_PLUGIN
#include "license/toolbox_utils.h" // 用于 toolbox license
#endif

#define PLUGIN_NAME "SymPy"

using namespace sympy;

// 插件初始化（可选）
int bxPluginInitLib(void *hdl)
{
#ifndef BEX_BUILD_PLUGIN
    return check_status(); // 检查 license
#else
    return 0;
#endif
}

// 注册类和算符
int bxPluginInit(int nrhs, const bxArray *prhs[])
{
    using namespace baltam::py;
#ifndef NDEBUG
    bxPrintf("Loading SymPy Plugin...\n");
#endif
    InitializePyRoot();
    if (initialize())
        return 1;

    bxOverloadFunction(PLUGIN_NAME, "sin", bxSymPySin);
    bxOverloadFunction(PLUGIN_NAME, "sinh", bxSymPySinh);
    bxOverloadFunction(PLUGIN_NAME, "asin", bxSymPyArcSin);
    bxOverloadFunction(PLUGIN_NAME, "asinh", bxSymPyArcSinh);
    bxOverloadFunction(PLUGIN_NAME, "cos", bxSymPyCos);
    bxOverloadFunction(PLUGIN_NAME, "cosh", bxSymPyArcCosh);
    bxOverloadFunction(PLUGIN_NAME, "acos", bxSymPyArcCos);
    bxOverloadFunction(PLUGIN_NAME, "acosh", bxSymPyArcCosh);
    bxOverloadFunction(PLUGIN_NAME, "tan", bxSymPyTan);
    bxOverloadFunction(PLUGIN_NAME, "tanh", bxSymPyTanh);
    bxOverloadFunction(PLUGIN_NAME, "atan", bxSymPyArcTan);
    bxOverloadFunction(PLUGIN_NAME, "tanh", bxSymPyTanh);
    bxOverloadFunction(PLUGIN_NAME, "exp", bxSymPyExp);
    bxOverloadFunction(PLUGIN_NAME, "log", bxSymPyLog);
    bxOverloadFunction(PLUGIN_NAME, "sqrt", bxSymPySqrt);
#ifndef NDEBUG
    bxPrintf("Done.\n");
#endif
    return 0;
}

int bxPluginFini()
{
    finalize();
    return 0;
}

// 注册插件中所有函数
static bexfun_info_t flist[] = {
    {"sym", bxCreateSymbol,
     "创建一个新的符号。输入可以是 double 或者字符串。"},
    {"syms", bxCreateSymbols,
     "创建多个符号。输入是字符串。如果输出数目和输入不匹配，会报错。"},
    // {"symfunc", bxCreateSymPyFunction, "创建符号函数"},
    {"pprint", bxSymPyPrettyPrint, 0},
    {"sympy_sp", bxGetSymPy, ""},
    {"", nullptr, nullptr}};

bexfun_info_t *bxPluginFunctions() { return flist; }
